package com.crishne.anwo.listener;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class LoginSessionListener implements HttpSessionListener,HttpSessionAttributeListener {

	private Map<String, HttpSession> _userIdRefSessionMap = new HashMap<String, HttpSession>();
	
	@Override
	public void attributeAdded(HttpSessionBindingEvent arg0) {
		HttpSession session = arg0.getSession();
		if(session.getAttribute("userid") == null || "".equals(session.getAttribute("userid"))){
			return;
		}
		String stUserId = (String)session.getAttribute("userid");
		if(_userIdRefSessionMap.containsKey(stUserId)){
			HttpSession oldFaoSession = this._userIdRefSessionMap.get(stUserId);
			oldFaoSession.invalidate();
		}
		_userIdRefSessionMap.put(stUserId, session);
	}

	@Override
	public void attributeRemoved(HttpSessionBindingEvent arg0) {
		
	}

	@Override
	public void attributeReplaced(HttpSessionBindingEvent arg0) {
		
	}

	@Override
	public void sessionCreated(HttpSessionEvent arg0) {
		
	}

	@Override
	public void sessionDestroyed(HttpSessionEvent arg0) {
		
	}

}
